const express = require('express');
const app = express();
app.listen(3000,()=>{console.log('服务器已开启')});
const path = require('path');
const fs = require('fs');
const time = require('time-stamp')
// 引入
const formidable = require('formidable');

// 路由
app.post('/upload',(req,res)=>{
    // 接收文件
    const hei = formidable({
        uploadDir:path.join(__dirname,'upload'),
        keepExtensions:true
    })
    hei.parse(req,(err,fields,files)=>{
        console.log(err,fields,files);

        if(err){
            res.send(err);
            return;
        } 
        let  oldpath = files.haha.filepath;
        let newname = time('YYYYMMDDHHssmm') + (Math.random()+'').substring(2,10) + path.extname(oldpath);

        let newpath = path.join(__dirname,'upload',newname); 
        fs.renameSync(oldpath,newpath)
        res.send('上传成功')
         

    })

})